package com.meme.editor.property;

import org.apache.log4j.Logger;

import com.meme.editor.EntityObject;


import com.meme.editor.property.value.NumberPropertyValue;

import javax.swing.JLabel;
import javax.swing.JComponent;

public class Property 
{
	static Logger logger = Logger.getLogger (Property.class);
	
	PropertyGroup mGroup;
	
	String mName;
	
	// now always same as mName
	String mLabel;	

	String mValueType;
	
	PropertyValue mValue;
	
	
	boolean mEditable;
	
//	public Property (String name)
//	{
//		this (name, null);
//	}
	
	public Property (PropertyGroup group, String name, String valueType)
	{
		setGroup ( group );
		mName = name;
		mLabel = name;
		
		mValueType = valueType;
		
		if ( valueType == null )
			mValue = new NumberPropertyValue (this); // default
		else
			mValue = PropertyFactory.getPropertyValue(valueType, this);
	}
	
	public String getName ()
	{
		return mName;
	}
	
	public String getLabel ()
	{
		return mLabel;
	}
	
	public String getValueType ()
	{
		return mValueType;
	}
	
	public void setControlType (String controlType)
	{
		mValue.setControlType (controlType);
	}
	
	public void setParameter (String key, Object param)
	{
		mValue.setParameter (key, param);
	}
	
	public void modifyValue (Object newValue)
	{
		mValue.modify (newValue);
	}
	
	public PropertyValue getValueComponent ()
	{
		return mValue;
	}

	public Object getValue ()
	{
		return mValue.getValue();
	}
	
	public String getValueAsString ()
	{
		return mValue.getValueAsString();
	}
	
	public void setEditable (boolean editable)
	{
		mEditable = editable;
	}
	
	public boolean isEditable ()
	{
		return mEditable;
	}
	
	protected void setGroup (PropertyGroup group)
	{
		mGroup = group;
	}
	
	protected PropertyGroup getGroup ()
	{
		return mGroup;
	}
	
	public String getGroupName ()
	{
		return getGroup ().getName ();
	}
	
	public EntityObject getOwner ()
	{
		return getGroup ().getGroupSet ().getOwner ();
	}
	
//	public JLabel getLabelComponent ()
//	{
//		return new JLabel (mLabel + ": ");
//	}
	
	public JComponent getControlComponent ()
	{
		return mValue.getComponent();
	}
	
	public void disposeControlComponent ()
	{
		mValue.disposeComponent ();
	}

	public int getControlComponentGridHeight ()
	{
		return mValue.getControl().getGridHeight();
	}
	

//---------------------------------------------------------------------------
	
	// for debug
	protected void dump ()
	{
		logger.debug ("  - Property: name=" + mName + ", value=" + mValue.toString() );
	}		

}
